#include "information_change.h"
#include "ui_information_change.h"

information_change::information_change(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::information_change)
{
    ui->setupUi(this);
    this->setLayout(ui->verticalLayout);
    this->setFixedSize(600,800);
    // 创建数据模型
        model = new QStandardItemModel(this);
        setupModel();


        setupTreeView();  // 新增调用 setupTreeView 函数
            // 连接信号和槽
            setupConnections();
}

information_change::~information_change()
{
    delete ui;
}
void information_change::setupModel()
{
    /*
    QFile file("information.txt");
       if (file.open(QIODevice::ReadOnly | QIODevice::Text))
       {
           QTextStream in(&file);

           QString email;
           QString phone;

           while (!in.atEnd())
           {
               in>>phone;
               in>>email;
               // 可以添加更多判断用于提取其他信息，这里只关注邮箱和电话
           }
           file.close();
           */
    model->setColumnCount(2);
        QStringList headers;
        headers << "标签" << "信息";
        model->setHorizontalHeaderLabels(headers);

        QList<QStandardItem *> student1;
        student1 << new QStandardItem("姓名") << new QStandardItem("张三");
        model->appendRow(student1);

        QList<QStandardItem *> student2;
        student2 << new QStandardItem("学号") << new QStandardItem("8209230166");
        model->appendRow(student2);

        QList<QStandardItem *> student3;
        student3 << new QStandardItem("学院") << new QStandardItem("计算机学院");
        model->appendRow(student3);

        QList<QStandardItem *> student4;
        student4 << new QStandardItem("电话") << new QStandardItem("19579511637");
        model->appendRow(student4);


        QList<QStandardItem *> student5;
        student5 << new QStandardItem("Email") << new QStandardItem("3362306665@qq.com");
        model->appendRow(student5);





}
void information_change::setupTreeView()
{
    ui->treeView->setModel(model);
    // 隐藏行号
       ui->treeView->header()->hide();
    ui->treeView->header()->setSectionResizeMode(QHeaderView::Stretch);
    ui->treeView->setEditTriggers(QTreeView::DoubleClicked | QTreeView::SelectedClicked);
    ui->treeView->setSelectionBehavior(QTreeView::SelectRows);
}
void information_change::setupConnections()
{
    connect(ui->treeView->model(), &QStandardItemModel::dataChanged, [this](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles) {
        if (roles.contains(Qt::EditRole)) {
            int column = topLeft.column();
            if (column == 2 || column == 3) {  // 电话或邮箱列
                QModelIndex index = ui->treeView->model()->index(topLeft.row(), column);
                QString newValue = ui->treeView->model()->data(index, Qt::EditRole).toString();
                // 在这里可以添加代码将修改后的值更新到实际存储数据的地方（比如数据库）
                //qDebug() << "修改后的值：" << newValue;
            }
        }
    });
}

void information_change::on_returnButton_clicked()
{
    this->close();
    home*h=new home();
    h->show();
}
